/**
 * This file is part of the CAKIS project.
 * 
 * Copyright (c) 2009-2010 - Florian Arndt, Christian Kerl
 * 
 * @license GNU Lesser General Public License v3 <http://www.opensource.org/licenses/lgpl-3.0.html>
 * @since   11.05.2010
 * @version SVN: $Id: ClientSessionAction.java 121 2010-05-11 20:12:04Z christian-kerl@web.de $
 */
package org.cakis.server.action.impl;

import java.util.Iterator;
import java.util.List;

import org.cakis.server.Names;
import org.cakis.server.service.session.ClientSessionInfo;
import org.cakis.server.service.session.ClientSessionManager;
import org.cakis.server.service.session.ClientSessionRedirectEvent;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.faces.Redirect;
import org.jboss.seam.framework.Controller;

/**
 * ClientSessionAction
 *
 * @author Christian Kerl
 */
@Name("mySessions")
@Scope(ScopeType.SESSION)
public class ClientSessionAction extends Controller {
    private static final long serialVersionUID = 1L;

    @In(Names.ClientSessionManager)
    private ClientSessionManager manager;

    private ClientSessionRedirectEvent lastProcessedEvent;

    @RequestParameter("redirectEventId")
    private String redirectEventId;

    @In
    private Redirect redirect;

    public Integer getCount() {
        return manager.getClientSessions().size();
    }

    public List<ClientSessionInfo> getSessions() {
        return manager.getClientSessions();
    }

    @Create
    public void create() {
        lastProcessedEvent = getLastEvent();
    }

    @Factory(value="myNextEventId", scope = ScopeType.EVENT)
    public String getNextEventId() {
        String result = "";
        ClientSessionRedirectEvent event = getLastEvent();

        if(event != null && !event.equals(lastProcessedEvent)) {
            result = event.getId().toString();

            lastProcessedEvent = event;
        }

        info("Next ClientSessionRedirectEvent[id=#0]", result);

        return result;
    }

    public String getNextEventDescription() {
        String result = null;

        if(lastProcessedEvent != null) {
            result = lastProcessedEvent.getSession().getClientId() + ": " + lastProcessedEvent.getDescription();
        }

        return result;
    }

    public void redirect() {
        if(redirectEventId != null) {
            ClientSessionRedirectEvent event = getRedirectEventById(redirectEventId);
            event.apply(redirect);

            info("Redirect #0", event);

            redirect.execute();
        }
    }

    private ClientSessionRedirectEvent getLastEvent() {
        return manager.getEvents().size() > 0 ?manager.getEvents().getLast() : null;
    }

    private ClientSessionRedirectEvent getRedirectEventById(String id) {
        Iterator<ClientSessionRedirectEvent> it = manager.getEvents().descendingIterator();

        ClientSessionRedirectEvent result = null;

        while(it.hasNext()) {
            ClientSessionRedirectEvent next = it.next();

            if(next.getId().equals(id)) {
                result = next;
                break;
            }
        }

        return result;
    }
}
